package jdbc;

import javax.swing.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

/**
 * 使用JDBC连接 java数据库链接流程
 * 1加载驱动
 * 使用DriverManager（JDBC提供的）再通过加载的驱动与数据库建立连接：Connection对象
 *
 */
public class JDBCDemo1 {
    public static void main(String[] args) {
        /*
        *
        * */
        //jdbc:mysql://localhost:3306/数据库名？一堆参数

        try {
            //1.加载驱动（mysql驱动）
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection connection = DriverManager.getConnection(
              "jdbc:mysql://localhost:3306/tedu?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true",
                    "root",
                    "root");
            System.out.println("连接数据库成功!");
            //换行符可以不删除  或者换行符可以换成空格
            //3
            Statement statement = connection.createStatement();
            String sql ="create table user( " +
                    "    id int primary key  auto_increment, " +
                    "    username varchar(30), " +
                    "    password varchar(30), " +
                    "    nickname varchar(30), " +
                    "    age int(3) " +
                    ")";
            /*
            *  Statement 提供的方法:
            *  public boolean execute(String sql)
            *
            * */
            statement.execute(sql);
            System.out.println("创建完毕");
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
